package com.controller;
import com.dao.BookReviewDao;
import com.dao.SubscribeDao;
import com.dao.UserDao;
import com.pojo.BookReview;
import com.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import static com.utils.MessageUtils.*;

@RestController
@RequestMapping("/bookReview")
public class BookReviewController{

    @Autowired
    private BookReviewDao bookReviewDao;

    @Autowired
    private SubscribeDao subscribeDao;

    @Autowired
    private UserDao userDao;

    @PostMapping
    public Result add(@RequestBody BookReview bookReview, HttpServletRequest request){
        int userId = (int) request.getSession().getAttribute("user");
        bookReview.setUserId(userId);
        bookReview.setBookName("《"+bookReview.getBookName()+"》");
        bookReview.setTime(LocalDate.now().toString());
        bookReviewDao.add(bookReview);
        return Result.success(ADD_DATA_SUCCESS);
    }

    @GetMapping
    public Result queryPage(int page,int pageSize,String queryString,HttpServletRequest request){
        String userId = request.getSession().getAttribute("user").toString();
        page=--page*pageSize;
        List<Map> maps;
        if (StringUtils.isEmpty(queryString)){
             maps= bookReviewDao.queryPage(page, pageSize);
        }else {
             queryString="%"+queryString+"%";
             maps= bookReviewDao.queryPageByParam(page, pageSize,queryString);
        }
        List<Integer> subscribeUserIds = subscribeDao.getSubscribeUserIds(userId);
        for (Map map : maps) {
            String id = map.get("userId").toString();
            map.put("nickName",userDao.getBYIdOrEMail(id).getNickName());
            map.put("isSubscribe","false");
            if (userId.equals(id)){
                map.put("isSubscribe","wo");
            }
            for (Integer subscribeUserId : subscribeUserIds) {
                if (id.equals(subscribeUserId.toString())){
                    map.put("isSubscribe","true");
                    break;
                }
            }
        }
        int total = bookReviewDao.getCount();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",maps);
        return Result.success(SELECT_DATA_SUCCESS,map);
    }

    @GetMapping("/getByIdAndSubscribe")
    public Result getByIdAndSubscribe(HttpServletRequest request){
        String userId =request.getSession().getAttribute("user").toString();
        List<Integer> userIds = subscribeDao.getSubscribeUserIds(userId);
        userIds.add(Integer.parseInt(userId));
        List<Map> maps = bookReviewDao.getByUserIds(userIds);
        pullNickName(maps);
        return Result.success(SELECT_DATA_SUCCESS,maps);
    }

    @GetMapping("/queryByBookName")
    public Result queryByBookName(String bookName){
        bookName="%"+bookName+"%";
        List<Map> maps = bookReviewDao.getByBookName(bookName);
        pullNickName(maps);
        return Result.success(SELECT_DATA_SUCCESS,maps);
    }

    @GetMapping("/getLikeBooks")
    public Result getLikeBooks(){
        List<String> likeBooks = userDao.getLikeBooks();
        return Result.success(SELECT_DATA_SUCCESS,likeBooks);
    }

    @GetMapping("/getByRandom")
    public Result getByRandom(){
        int total = bookReviewDao.getCount();
        int i = new Random().nextInt(total);
        i=i==0?1:i;
        Map map = bookReviewDao.queryPage(i - 1, 1).get(0);
        String nickName = userDao.getBYIdOrEMail(map.get("userId").toString()).getNickName();
        map.put("nickName",nickName);
        return Result.success(SELECT_DATA_SUCCESS,map);
    }

    private void pullNickName(List<Map> maps){
        maps.stream().forEach(map->{
            String id = map.get("userId").toString();
            map.put("nickName",userDao.getBYIdOrEMail(id).getNickName());
        });
    }
}
